﻿using System.Windows;
using System.Windows.Controls;

namespace WpfApplication5
{
    public class ContentControlEx:ContentControl
    {
        public static readonly DependencyProperty TemplateSelectorProperty =
               DependencyProperty.Register("TemplateSelector", typeof(DataTemplateSelector), typeof(ContentControlEx), new PropertyMetadata((PropertyChangedCallback)null));

        public DataTemplateSelector TemplateSelector
        {
            get
            {
                return (DataTemplateSelector)this.GetValue(ContentControlEx.TemplateSelectorProperty);
            }
            set
            {
                this.SetValue(ContentControlEx.TemplateSelectorProperty, (object)value);
            }
        }

        static ContentControlEx()
        {
        }

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            if (this.TemplateSelector == null)
                return;
            this.ContentTemplate = this.TemplateSelector.SelectTemplate(newContent, (DependencyObject)this);
        }
    }
}
